#include "stdafx.h"
#include "GameMap.h"
#include "Tools.h"
#include <fstream> 
using namespace std;

CGameMap::CGameMap()
{
}


CGameMap::~CGameMap()
{
}


void CGameMap::Init()
{
	m_StaticBack = cvLoadImage(".\\map\\back1.jpg", 1);
	m_Background = cvCloneImage(m_StaticBack);
	m_ShowImg = cvCloneImage(m_StaticBack);
	char name1[] = ".\\map\\obstacle0.jpg";
	for (int i = 0; i < 3; i++)
	{
		sprintf_s(name1, ".\\map\\obstacle%d.jpg", i);
		m_ObstacleImg[i] = cvLoadImage(name1);
	}
}


void CGameMap::ReadMaptxt()
{
	ifstream txt;
	txt.open(".//map//map_lever1.txt");

	int k;
	for (int i = 0; i < MAP_SIZE; i++)
	{
		for (int j = 0; j < MAP_SIZE; j++)
		{
			txt >> k;
			m_Map[i][j] = k;
		}
	}
	
}

void CGameMap::CreateMap()
{
	for (int i = 0; i < MAP_SIZE; i++)
	{
		for (int j = 0; j < MAP_SIZE; j++)
		{
			int x = i * 40;
			int y = j * 40;
			int k = m_Map[i][j];
			if (k != -1)
				Copy2Back(m_ObstacleImg[k], m_Background,x, y, 235, 235, 235);
		}
	}
}

IplImage * CGameMap::GetBackShowImg()
{
	CreateMap();
	cvCopy(m_Background, m_ShowImg);
	return m_ShowImg;

}

void CGameMap::Release()
{
	cvReleaseImage(&m_StaticBack);
	cvReleaseImage(&m_Background);
	for (int i = 0; i < 3; i++)
	{
		cvReleaseImage(&m_ObstacleImg[i]);
	}
}